﻿<!---
 
  Copyright (c) 2010, Cristian Costantini - cristian@millemultimedia.it
  
  FileManager is licensed under the terms of the Open Source GPL 3.0 license. 
	
	http://www.gnu.org/licenses/gpl.html
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--->
<cfcomponent output="false" hint="I'm Facade Generator. Write a file with all service method.">

	<cffunction name="init" access="public" output="false" returntype="Component">
        <cfargument name="codeGenerator" type="fileManager.util.codeGenerator.CodeGenerator" />
		<cfscript>
		variables.codeGenerator = arguments.codeGenerator;
		
		</cfscript>
		<cfreturn this />
	</cffunction>
	
	<cffunction name="onPluginLoad" access="public" returntype="void">
		<cfset buildService( arguments.pluginConfig ) />
	</cffunction>

	<cffunction name="buildService" access="private" returntype="void" hint="Create a remote facade">
		<cfargument name="config" type="xml" required="true" />
		<cfset var services = xmlSearch( arguments.config, "/plugin/services/remote/") />
		<cfset var methodList = arrayNew(1) />
		<cfset var paramList = arrayNew(1) />
		<cfset var functions = arrayNew(1) />
		<cfset var method = structNew() />
		<cfset var params = arrayNew(1) />
		<cfset var item = 0 />
		
		<cfloop index="item" from="1" to="#arrayLen( services )#">
		
			<cfif services[ item ].XmlAttributes['type'] eq "facade">
			
				<cfset functions = services[ item ].XmlChildren />
				
				<cfloop index="func" from="1" to="#arrayLen( functions )#">
				
					<cfset methodList.add( functions[ func ].XmlAttributes['name'] ) />
					
				</cfloop>
				
				<cfparam name="services[ item ].XmlAttributes['use-service']" default="#services[ item ].XmlAttributes['name']#" />
				
				<cfset variables.codeGenerator.createRemoteFacade( services[ item ].XmlAttributes['map-to-class'], services[ item ].XmlAttributes['name'], services[ item ].XmlAttributes['use-service'], methodList ) />
			
			<cfelseif services[ item ].XmlAttributes['type'] eq "proxy">
			
				<cfset functions = services[ item ].XmlChildren />
				<cfset methodList = arrayNew(1) />
				
				<cfif arrayLen( functions ) gt 1>
				
					<cfthrow message="Set only one method for remote proxy." />
				
				</cfif>
				
				<cfloop index="func" from="1" to="#arrayLen( functions )#">
				
					<cfset params = functions[ func ].XmlChildren />
					
					<cfset paramList = arrayNew(1) />
				
					<cfloop index="para" from="1" to="#arrayLen( params )#" >
					
						<cfset paramList.add( params[ para ].XmlAttributes ) />
					
					</cfloop>

					<cfset method[ 'name' ] = functions[ func ].XmlAttributes['name'] />
					<cfset method[ 'parameters' ] = paramList />
					
					<cfset methodList.add( method ) />
					
				</cfloop>
				
				<cfset variables.codeGenerator.createRemoteProxy( services[ item ].XmlAttributes['name'], services[ item ].XmlAttributes['use-service'], methodList ) />
				
			</cfif>
		
		</cfloop>

	</cffunction>

</cfcomponent>